﻿#include <QMouseEvent>
#include <QtMath>
#include <QPainter>
#include <QMenu>
#include <QMessageBox>
#include <windows.h>
#include "CRobMapPlan.h"
#include "CHeader.h"
#include "CPolItem.h"
#include "CSqlOper.h"

CRobMapPlan::CRobMapPlan(QWidget *parent)
	: CMapWidgetBase(parent)
{
	ui.setupUi(this);
	m_bSketMoveAble = false;
	m_nChoseIndex = -1;
	m_nChoseXVal = -1;
	m_scale = 1;
	m_nXval = 0;
}

CRobMapPlan::~CRobMapPlan()
{}

void CRobMapPlan::setPoints(const QVector<QPointF> &points, QVector<int> vecXData)
{
	//如果是空数组 通知上层 添加一个原点
	if (points.size() == 0)
	{
		QPointF pCurrPoint(0, 0);
		emit sigAddPoint(pCurrPoint,0);
		return;
	}
	clearPoints();//清空Points
	m_translation.setX(0);
	m_translation.setY(0);
	m_vecOripoints = points;
	m_vecXdata = vecXData;
	GetXYMaxMin();
	update();
}

void CRobMapPlan::setActPoints(int nXVal, bool bCharge /*= false*/)
{
	if (nXVal < 0)
		nXVal = 0;
	m_bFlicker = !m_bFlicker;
	m_bChargeFlicker = bCharge;//当前的点是否是充点电
	int nDifference = nXVal - m_nXval;
	if (abs(nDifference) > 20)
	{
		if (nDifference > 0)
		{
			//满足添加巡检点条件
			if (m_dRotateAngleOld != m_dRotateAngle && ((m_nXval - m_vecXdata[m_vecXdata.size() - 1]) > 1000))
			{
				emit sigAddPoint(m_cRobIncotion.m_dOriPoint, m_nXval);
				
			}
			m_dRotateAngleOld = m_dRotateAngle;
		}
		else
		{
			m_dRotateAngle = m_dRotateAngleOld;
		}
	}
	m_nXval = nXVal;
	GetXYMaxMin();
	GetDrawActPoint();
	update();
}

void CRobMapPlan::addPoint()
{
    //判断是不是已经是巡检点了 如果是不允许添加
	for (int i = 0; i < m_vecZoomPoint.size(); i++)
	{
		QPoint point(m_cRobIncotion.m_dZoomPoint.x(), m_cRobIncotion.m_dZoomPoint.y());
		if (isPointInCir(point, m_vecZoomPoint[i]))
		{
			messageBox(0, A2T("不允许重复添加巡检点"));
			return;
		}
	}
	emit sigAddPoint(m_cRobIncotion.m_dOriPoint, m_nXval);		
}

void CRobMapPlan::GetDrawActPoint()
{
	m_cWidgetSize = QWidget::size();
	if (m_cWidgetSize.width() <= 0 || m_cWidgetSize.height() <= 0)
	{
		return;
	}
	//清空画图数组，同时将转换过的坐标点的数组大小改成和原始坐标点的大小一致
	if (m_vecPoints.size() != m_vecOripoints.size())
	{
		m_vecPoints.clear();
		m_vecPoints.shrink_to_fit();
		m_vecPoints.resize(m_vecOripoints.size());
	}
	m_vecActPoints.clear();
	m_vecActPoints.shrink_to_fit();
	m_vecZoomPoint.resize(m_vecPoints.size());

	if (m_vecXdata.size() > 0)
	{
		if (m_nXval < m_vecXdata[0])
		{
			m_nXval = m_vecXdata[0];
		}
	}

	//计算所有已经执行过的点
	//获取已经执行到的点
	for (int i = 0; i < m_vecOripoints.size(); i++)
	{
		if (m_nXval > m_vecXdata[i])
		{
			m_vecActPoints.push_back(m_vecOripoints[i]);

			//如果已经是最后一个点了
			if (i == (m_vecOripoints.size() - 1))
			{
				double radians = qDegreesToRadians((double)m_dRotateAngleOld);// 将角度转换为弧度
				double dXVal = m_nXval - m_vecXdata[m_vecXdata.size() - 1];//  超过最后一个点的距离
				double dAddX = dXVal * qCos(radians) + m_vecOripoints[m_vecOripoints.size() - 1].x();//X增量
				double dAddY = dXVal * qSin(radians) + m_vecOripoints[m_vecOripoints.size() - 1].y();//Y增量
				QPointF cPoint(dAddX, dAddY);
				m_vecActPoints.push_back(cPoint);
			}
		}
		else if (m_nXval == m_vecXdata[i])
		{
			m_vecActPoints.push_back(m_vecOripoints[i]);
			break;
		}
		else
		{
			if (m_vecXdata.size() > 1)
			{
				double lengthAB = m_vecXdata[i] - m_vecXdata[i - 1];
				double dABVal = (m_nXval - m_vecXdata[i - 1]) / lengthAB;
				// 计算点C在向量AB上的坐标
				qreal x3 = m_vecOripoints[i - 1].x() + (m_vecOripoints[i].x() - m_vecOripoints[i - 1].x())*dABVal;
				qreal y3 = m_vecOripoints[i - 1].y() + (m_vecOripoints[i].y() - m_vecOripoints[i - 1].y())*dABVal;
				m_vecActPoints.push_back(QPointF(x3, y3));
				break;
			}
		}
	}
	m_cRobIncotion.m_dOriPoint = m_vecActPoints[m_vecActPoints.size() - 1];

	double dScale = qMax(double(m_nXMax - m_nXMin) / m_cWidgetSize.width(), double(m_nYMax - m_nYMin) / m_cWidgetSize.height());
	m_DrawLine.m_dConvertPoint.setX(-1);
	m_DrawLine.m_dConvertPoint.setY(-1);

	//计算画图点坐标
	for (int i = 0; i < m_vecPoints.size(); i++)//这时m_vecOripoints里的点的xy坐标已经变成了缩放后的坐标
	{
		double dX = (m_vecOripoints[i].x() - m_nXMin) / dScale;
		double dY = (m_vecOripoints[i].y() - m_nYMin) / dScale;
		m_vecPoints[i].setX(dX*0.8 + m_cWidgetSize.width()*0.1);
		m_vecPoints[i].setY(dY*0.8 + m_cWidgetSize.height()*0.1);
	}
	for (int i = 0; i < m_vecActPoints.size(); i++)//这时m_vecActPoints里的点的xy坐标已经变成了缩放后的坐标
	{
		double dX = (m_vecActPoints[i].x() - m_nXMin) / dScale;
		double dY = (m_vecActPoints[i].y() - m_nYMin) / dScale;
		m_vecActPoints[i].setX(dX*0.8 + m_cWidgetSize.width()*0.1);
		m_vecActPoints[i].setY(dY*0.8 + m_cWidgetSize.height()*0.1);
	}
	m_cRobIncotion.m_dConvertPoint = m_vecActPoints[m_vecActPoints.size() - 1];//这时机器人当前位置的坐标已经变成了缩放后的坐标

	//计算虚线点
	if (m_nXval >= m_vecXdata[m_vecXdata.size() - 1])//大于 最后一个巡检点
	{
		double radians = qDegreesToRadians((double)m_dRotateAngle);// 将角度转换为弧度
		double dAddX = (m_nXMax - m_nXMin)*SketcgLength * qCos(radians) + m_cRobIncotion.m_dOriPoint.x();//X增量
		double dAddY = (m_nXMax - m_nXMin)*SketcgLength * qSin(radians) + m_cRobIncotion.m_dOriPoint.y();//Y增量
		double dX = (dAddX - m_nXMin) / dScale;
		double dY = (dAddY - m_nYMin) / dScale;
		m_DrawLine.m_dConvertPoint.setX(dX*0.8 + m_cWidgetSize.width()*0.1);
		m_DrawLine.m_dConvertPoint.setY(dY*0.8 + m_cWidgetSize.height()*0.1);
	}
	else
	{
		if (m_vecPoints.size() >= 2)
		{
			QPointF pLast = m_vecPoints[m_vecPoints.size() - 1];
			QPointF pNext = m_vecPoints[m_vecPoints.size() - 2];
			m_dRotateAngle=GetAngel(pNext, pLast);
			m_dRotateAngleOld = m_dRotateAngle;
		}

	}
	getDrawZoomElse();
}

void CRobMapPlan::paintEvent(QPaintEvent *event)
{
	QMutexLocker loker(&m_MutexOriData);//智能锁 addwxy

	if (m_vecPoints.size() < 1 || m_vecActPoints.size() < 1)
		return;
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	QRectF m_cRectE;
	m_cRectE.setRect(10, 10, 90, 15);
	QString sData = "";
	if (m_dRotateAngle < 10)
	{
		sData = A2T("角度:  ") + QString::number(m_dRotateAngle) + A2T("度");
	}
	else if (m_dRotateAngle < 100)
	{
		sData = A2T("角度: ") + QString::number(m_dRotateAngle) + A2T("度");
	}
	else
	{
		sData = A2T("角度:") + QString::number(m_dRotateAngle) + A2T("度");
	}
	QFont f = QFont("Microsoft YaHei", 10, QFont::Bold);
	painter.setFont(f);
	painter.setPen(QColor("#000000"));
	painter.drawText(m_cRectE, Qt::AlignCenter, sData);
	// Scale and translate the coordinate system
	painter.translate(0, 0);
	painter.scale(m_scale, m_scale);
	painter.translate(m_translation);

	// 画线段
	painter.setPen(QPen(QColor(82, 91, 105), DiamSmall / 2));
	for (int i = 0; i < m_vecPoints.size() - 1; i++)
	{
		painter.drawLine(m_vecPoints[i], m_vecPoints[i + 1]);
	}
	//画圆
	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(QColor(147, 147, 147)));
	for (const QPointF &point : m_vecPoints) {
		QRect rect1 = QRect(point.x() - DiamSmall / 2, point.y() - DiamSmall / 2, DiamSmall, DiamSmall);
		painter.drawEllipse(rect1);
	}
	//画线段
	painter.setPen(Qt::NoPen);
	painter.setPen(QPen(QColor(0, 205, 255), DiamSmall / 2));
	if (m_vecActPoints.size() > 0)
	{
		for (int i = 0; i < m_vecActPoints.size() - 1; i++)
		{
			painter.drawLine(m_vecActPoints[i], m_vecActPoints[i + 1]);
		}
		painter.drawLine(m_cRobIncotion.m_dConvertPoint, m_vecActPoints[m_vecActPoints.size() - 1]);

		if (m_DrawLine.m_dConvertPoint.x() != -1 && m_DrawLine.m_dConvertPoint.y() != -1)
		{
			//画示意图
			painter.setPen(QPen(QColor(255, 80, 0), DiamSmall / 2, Qt::DotLine));
			painter.drawLine(m_cRobIncotion.m_dConvertPoint, m_DrawLine.m_dConvertPoint);
		}

	}


	//画圆
	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(QColor(255, 255, 255)));
	for (int i = 0; i < m_vecActPoints.size(); i++)
	{
		QRect rect1 = QRect(m_vecActPoints[i].x() - DiamLarge / 2, m_vecActPoints[i].y() - DiamLarge / 2, DiamLarge, DiamLarge);
		painter.drawEllipse(rect1);
	}
	painter.setPen(Qt::NoPen);
	painter.setBrush(QBrush(QColor(53, 215, 255)));
	for (int i = 0; i < m_vecActPoints.size(); i++)
	{
		QRect rect1 = QRect(m_vecActPoints[i].x() - DiamSmall / 2, m_vecActPoints[i].y() - DiamSmall / 2, DiamSmall, DiamSmall);
		painter.drawEllipse(rect1);
	}
	//画选中的圆
	if (m_nChoseIndex >= 0)
	{
		if (m_vecPoints.size() > m_nChoseIndex)
		{
			painter.setPen(Qt::NoPen);
			painter.setBrush(QBrush(QColor(255, 0, 0)));
			{
				QRect rect1 = QRect(m_vecPoints[m_nChoseIndex].x() - DiamSmall / 2, m_vecPoints[m_nChoseIndex].y() - DiamSmall / 2, DiamSmall, DiamSmall);
				painter.drawEllipse(rect1);
			}
			m_nChoseXVal = m_vecXdata[m_nChoseIndex];
		}
	}

	QPointF cPoint = m_cRobIncotion.m_dConvertPoint;
	//画光环
	if (m_bFlicker)
	{
		QRect rect3 = QRect(cPoint.x() - DiamGuanghuanLarge / 2, cPoint.y() - DiamGuanghuanLarge / 2, DiamGuanghuanLarge, DiamGuanghuanLarge);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(9, 85, 118, 120)));
		painter.drawEllipse(rect3);

		QRect rect2 = QRect(cPoint.x() - DiamGuanghuanSmall / 2, cPoint.y() - DiamGuanghuanSmall / 2, DiamGuanghuanSmall, DiamGuanghuanSmall);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(102, 149, 168, 120)));
		painter.drawEllipse(rect2);

		QRect rect4 = QRect(cPoint.x() - DiamGuanghuan / 2, cPoint.y() - DiamGuanghuan / 2, DiamGuanghuan, DiamGuanghuan);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(12, 151, 193, 180)));
		painter.drawEllipse(rect4);

		QRect rect = QRect(cPoint.x() - DiamLarge / 2, cPoint.y() - DiamLarge / 2, DiamLarge, DiamLarge);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(255, 255, 255)));
		painter.drawEllipse(rect);

		QRect rect1 = QRect(cPoint.x() - DiamSmall / 2, cPoint.y() - DiamSmall / 2, DiamSmall, DiamSmall);
		painter.setPen(Qt::NoPen);
		painter.setBrush(QBrush(QColor(53, 215, 255)));
		painter.drawEllipse(rect1);

		QRect rectJqr = QRect(cPoint.x() - 18, cPoint.y() - 35, 36, 36);
		painter.setBrush(QBrush(QColor(0, 0, 0, 0)));
		painter.drawRect(rectJqr);
		painter.drawPixmap(rectJqr, QPixmap(":/icon/Rob/Rob.ico"));
	}
}

void CRobMapPlan::mousePressEvent(QMouseEvent *event)
{
	bool bIsOnPoint = false;   //是否在巡检点上面点击
	int  nIndex = 0;
	if (event->button() == Qt::LeftButton)
	{
		m_lastPos = event->pos();

		for (int i = 0; i < m_vecPoints.size(); i++)
		{
			if (isPointInCir(m_lastPos, m_vecZoomPoint[i]))
			{
				bIsOnPoint = true;
				m_nChoseIndex = i;
				m_nChoseXVal = m_vecXdata[i];
				emit(sigSelectedPoint(m_vecXdata[m_nChoseIndex]));
			}
		}
		if (!bIsOnPoint)
		{
			m_isDragging = true;
		}
		update();
	}

	if (event->button() == Qt::RightButton)
	{
		QPoint point = event->pos();

		for (int i = 0; i < m_vecPoints.size(); i++)
		{
			if (isPointInCir(point, m_vecZoomPoint[i]))
			{
				bIsOnPoint = true;
				nIndex = i;

			}
		}
		if (!bIsOnPoint)
		{
			if (isPointInCir(point, m_cRobIncotion))
			{
				QMenu menu(this);
				QAction aAdd;
				aAdd.setText(A2T("设置为巡检点"));
				menu.addAction(&aAdd);
				QAction *pAction = menu.exec(QCursor::pos());
				if (pAction == &aAdd)//添加测点
				{
					emit sigAddPoint(m_cRobIncotion.m_dOriPoint, m_nXval);
				}
			}
			else
			{
				if (isPointInCir(point, m_DrawLine))
				{
					m_bSketMoveAble = true;
				}
			}

		}
		else
		{
			QMenu menu(this);
			QAction aRunPoint;
			QAction aSet;
			QAction aMove;
			QAction aCancel;
			aRunPoint.setText(A2T("运动到该点"));
			aSet.setText(A2T("编辑本巡检点"));
			aMove.setText(A2T("移动巡检点"));
			aCancel.setText(A2T("取消巡检点"));
			menu.addAction(&aRunPoint);
			menu.addAction(&aSet);
			menu.addAction(&aMove);
			menu.addAction(&aCancel);

			QAction *pAction = menu.exec(QCursor::pos());
			if (pAction == &aRunPoint)//运动到该点
			{
				if (nIndex >= 0)
				{
					emit sigMoveMent(m_vecXdata[nIndex]);
				}
			}
			else if (pAction == &aSet)//编辑本巡检点
			{
				if (nIndex >= 0)
				{
					emit sigEditPoint(m_vecXdata[nIndex]);
				}

			}
			else if (pAction == &aMove)//编辑本巡检点
			{
				if (nIndex >= 0)
				{
					emit sigMovePoint(m_vecXdata[nIndex]);
				}

			}
			else if (pAction == &aCancel)//取消巡检点
			{
				if (nIndex >= 0)
				{
					emit sigDelPoint(m_vecXdata[nIndex]);
				}
			}

		}
	}
}

void CRobMapPlan::mouseReleaseEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton)
	{
		m_isDragging = false;
	}
	if (event->button() == Qt::RightButton)
	{
		m_bSketMoveAble = false;
	}
	getDrawZoomElse();
}

void CRobMapPlan::mouseMoveEvent(QMouseEvent *event)
{
	if (m_isDragging)
	{
		QPoint delta = event->pos() - m_lastPos;
		m_translation += delta;
		m_lastPos = event->pos();
		update();
	}
	else
	{
		if (m_bSketMoveAble)
		{
			QPointF point = event->pos();
			double dAngle = GetAngel(m_cRobIncotion.m_dZoomPoint, point);//计算两点之间的角度
			int nAngle = dAngle;
			nAngle = nAngle / 5;
			m_dRotateAngle = nAngle * 5;
		}
	}
}



bool CRobMapPlan::isPointInCir(const QPoint &point, const PointData &rect)
{
	int x = point.x() - rect.m_dZoomPoint.x();
	int y = point.y() - rect.m_dZoomPoint.y();
	if (sqrt(pow(x, 2) + pow(y, 2)) <= static_cast<double>(rect.m_dRadius))
	{
		return true;
	}
	return false;
}


void CRobMapPlan::getDrawZoomElse()
{
	if (m_cWidgetSize.width() <= 0 || m_cWidgetSize.height() <= 0)
	{
		return;
	}

	for (int i = 0; i < m_vecPoints.size(); i++)
	{
		double dX = (m_vecPoints[i].x() + m_translation.x()) * m_scale;
		double dY = (m_vecPoints[i].y() + m_translation.y()) * m_scale;
		m_vecZoomPoint[i].m_dZoomPoint.setX(dX);
		m_vecZoomPoint[i].m_dZoomPoint.setY(dY);
		m_vecZoomPoint[i].m_dRadius = 2 * m_scale;//10->2 现场需求充电桩附近距离短也能添加巡检点
	}

	{
		double dX = (m_DrawLine.m_dConvertPoint.x() + m_translation.x()) * m_scale;
		double dY = (m_DrawLine.m_dConvertPoint.y() + m_translation.y()) * m_scale;
		m_DrawLine.m_dZoomPoint.setX(dX);
		m_DrawLine.m_dZoomPoint.setY(dY);
		m_DrawLine.m_dRadius = 60 * m_scale;
	}

	{
		double dX = (m_cRobIncotion.m_dConvertPoint.x() + m_translation.x()) * m_scale;
		double dY = (m_cRobIncotion.m_dConvertPoint.y() + m_translation.y()) * m_scale;
		m_cRobIncotion.m_dZoomPoint.setX(dX);
		m_cRobIncotion.m_dZoomPoint.setY(dY);
		m_cRobIncotion.m_dRadius = 10 * m_scale;
	}
}

double CRobMapPlan::GetAngel(QPointF p1, QPointF p2)
{
	double dAngel = 0;
	double reAngel = 0;
	if (p1.x() == p2.x())
	{
		if (p2.y() >= p1.y())
		{
			reAngel = 90;
		}
		else
		{
			reAngel = 270;
		}
	}
	else
	{
		dAngel = atan2((p2.y() - p1.y()), (p2.x() - p1.x()));
		reAngel = dAngel * (180 / PI);

		if (reAngel < 0)
			reAngel = reAngel + 360;
	}
	return reAngel;
}

